import json

from devicetest.api import Asserts
from devicetest.aw.OpenHarmony import CommonOH, WifiHelper
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import Setting


class PreTest_RK3568_Basic_Setting_0400(TestCase, Setting):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 进入桌面，点击设置-WLAN，点击关闭WLAN按钮
    # 预期结果：
    # WLAN按钮不亮，不展示任何WLAN列表，且断开已连接的wifi，通知栏的wifi连接的图标不再显示

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)
        Step('预置条件2：打开wifi')
        try:
            WifiHelper.openWifi(self.Phone1)
        except:
            pass

    def process(self):
        Step('步骤1：点击设置-WLAN，点击关闭WLAN按钮')
        CommonOH.startAbility(self.Phone1, self.ability_name, self.bundle_name)
        CommonOH.wait(self.Phone1, 2)
        CommonOH.checkIfTextExist(self.Phone1, '设置')
        CommonOH.touchByText(self.Phone1, 'WLAN')
        CommonOH.touchByType(self.Phone1, 'Toggle')
        Step('预期结果1：WLAN按钮不亮，不展示任何WLAN列表，且断开已连接的wifi，通知栏的wifi连接的图标不再显示')
        property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
        Asserts.assert_false(property_dict.get('checked'))
        CommonOH.checkIfTextExist(self.Phone1, '可用WLAN', expect=False)

    def teardown(self):
        common_teardown(self.Phone1, self.bundle_name)
        Step('收尾2：关闭wifi')
        try:
            WifiHelper.openWifi(self.Phone1)
        except:
            pass
